﻿/// <reference path="../jquery-1.8.2.intellisense.js" />
/// <reference path="../knockout-2.2.0.debug.js" />
; (function (root) {
    var app = root.app;
    var dataservice = app.dataservice;
    var submissions = [];
    var selectedIndex = 0;
   

    var vm = {
        Id: ko.observable(0),
        Content: ko.observable(""),
        Occasion: ko.observable(""),
        Origin:  ko.observable(""),
        Status:  ko.observable(""),
        Author:  ko.observable(""),
        Website: ko.observable(""),
        next: next,
        previous: previous,
        updateSubmissions: updateSubmissions,
        setSubmissions: setSubmissions,
        setViewData: setViewData,
        updateSelected: updateSelected

    };


    app.editViewModel = vm;

    function setSubmissions(data) {
        submissions = data;
        setViewData(data[0]);
    }

    function setViewData(e) {
        vm.Id(e.Id);
        vm.Content(e.Content);
        vm.Occasion(e.Occasion);
        vm.Origin(e.Origin);
        vm.Status(e.Status);
        vm.Author(e.Author);
        vm.Website(e.Website);
    }

    function updateSelected() {
        submissions[selectedIndex] = {
            Id: vm.Id(),
            Content: vm.Content(),
            Occasion: vm.Occasion(),
            Origin: vm.Origin(),
            Status: vm.Status(),
            Author: vm.Author(),
            Website: vm.Website()
                   
        }
    }


    function next() {

        if (submissions.length == 1) return;
        
        root.$("#toast-content").fadeTo('fast', 0.30, function () {
            updateSelected();
            selectedIndex += 1;
            if (selectedIndex == submissions.length) {
                selectedIndex = 0;
            }
            setViewData(submissions[selectedIndex])

             root.$("#toast-content").fadeTo('fast', 1.0);
         });
        
    }

    function previous() {

        if (submissions.length == 1) return;

       root.$("#toast-content").fadeTo('fast', 0.30, function () {
        updateSelected();
        selectedIndex -= 1;

        if (selectedIndex == -1) {
            selectedIndex = submissions.length - 1;
        }
        
        setViewData(submissions[selectedIndex]);
        root.$("#toast-content").fadeTo('fast', 1.0);
        });
    }

    function updateSubmissions() {
        updateSelected();
        dataservice.updateSubmissions(submissions);
    }


}(window));